<template>
	<div class="edit-page" v-loading="isLoading">
		<div class="edit-header">
			<span class="title">编辑试题</span>
			<el-button type="primary" @click="onGoToList">返回</el-button>
		</div>
		<div class="height-12" />
		<edit-frame
			ref="editFrameRef"
			type="edit"
			class="edit-content"
			:parent-resource-id="parentResourceId"
			:question-data="initData"
			:parent-type="parentType"
			@edit-next="editNext"
		/>
	</div>
</template>
<script>
	import EditFrame from "../components/edit-frame/index.vue";
    import questionApi from "@rc/apis/rc/question.mjs";
	export default {
        name:'edit-question',
		components: { EditFrame},
        data() {
            return {
                initData: {},//编辑组件入参
                resourceInfo:{},//试题详情
                resourceId:'',//试题标识
                parentResourceId:'',//代表当前是编辑小题对应的--大题id
                parentType:'',//当编辑子题时，大题的类型
                questionType:'',//试题标识
                resourceIds:'',//编辑的试题列表资源id数组
                combinedType: ["A", "CF", "L", "C"], //组合题型
                isLoading:false
            };
        },
        created() {
            const {resourceId,questionType,parentResourceId,parentType} = this.$route.query;
            this.resourceId = resourceId;
            this.questionType = questionType;
            this.parentResourceId = parentResourceId;
            this.parentType = parentType;
            //获取试题列表
            const editIds = localStorage.getItem('rc-editIds');
            if(editIds){
                this.resourceIds =JSON.parse(editIds);
            }

            if(!this.parentType){//大题/独立题型
               this.queryResourceInfo();
            } else {//组合体下的小题
                this.querySmallResourceInfo();
            }
        },
        methods:{
            //返回试题管理页面
            onGoToList(){
                nlapp.TabManager.closeBack(`/rc/questions/list`);
            },
            //抽取题下的基本信息和标签信息
            getBasicAndTagInfo(resource){
                let {projectId,projectName,subjectId,subjectName,questionDeveloperId,questionDeveloperName,areaList,applicableYear,knowledgeList,labelList,chapterList} =  resource;
                //处理标签信息格式
                if(labelList){
                    labelList = labelList.map(label=>{
                        return {
                            labelId:label.labelId,
                            labelDetailId:label.labelDetailIds[0],
                        };
                    });
                } else {
                    labelList = [];
                }
                let basicInfo = {
                    projectId:[projectId],
                    projectName,
                    subjectId,
                    subjectName,
                    knowledgeList:knowledgeList||[],
                    questionDeveloperId,
                    questionDeveloperName,
                    chapterIds:chapterList?chapterList.map(chapter=>chapter.chapterId):[],
                    chapterList
                };
                let tagInfo = {
                    areaIds:areaList?areaList.map(area=>area.cityId):[],
                    areaList,
                    applicableYear,
                    labelList
                };
                return {
                    basicInfo,
                    tagInfo
                };
            },
            //获取试题信息详情
            queryResourceInfo(){
                const {resourceId,combinedType} = this;
                this.isLoading = true;
                questionApi.queryResourceInfo({
                    data: {
                        resourceId
                    },
                    success: (resource) => {
                    //处理子题的数据结构
                    if(combinedType.includes(resource.questionType)){
                        resource.smallQuestionList = resource.smallQuestionList.map(sq=>{
                            let  data = this.getBasicAndTagInfo(sq);
                            //子题处理成可以直接向后端传参的形式
                            return {
                                ...sq,
                                ...data.basicInfo,
                                ...data.tagInfo
                            };
                        });
                    }
                    this.initData = {
                        ...resource,
                        ...this.getBasicAndTagInfo(resource)
                    };
                    console.log(this.initData);
                    },
                    complete: () => {
                         this.isLoading = false;
                    }
                });
            },
            //获取小题详细信息-单独编辑组合题下小题
            querySmallResourceInfo(){
                this.isLoading = true;
                questionApi.querySmallResourceInfo({
                    data: {
                        resourceId:this.resourceId
                    },
                    success: (resource) => {
                        this.initData = {
                            ...resource,
                            ...this.getBasicAndTagInfo(resource)
                        };
                    },
                    complete: () => {
                        this.isLoading = false;
                    }
                });
            },
            //提交并添加编辑一下题时按照试题列表的结果顺序自动进入下一题（该组合题的下一子题）
            editNext(){
              let index = this.resourceIds.findIndex(id=>id===this.resourceId);
              if(index===this.resourceIds.length-1){
                setTimeout(()=>{
                    this.$message.warning("已经是最后一题了");
                    return;
                },500);
                setTimeout(()=>{
                    nlapp.TabManager.closeBack(`/rc/questions/list`);
                },1000);
              } else {//取下一题
                    this.$refs.editFrameRef.resetForm();
                    this.resourceId = this.resourceIds[index+1];
                    this.resourceInfo={};
                    if(!this.parentType){//大题/独立题型
                        this.queryResourceInfo();
                    } else {//组合体下的小题
                        this.querySmallResourceInfo();
                    }
              }
            }
        }
	};
</script>
<style lang="less" scoped>
    .edit-page{
        height: 100%;
        display: flex;
        flex-direction: column;
    }
    .edit-header{
        flex-shrink: 0;
        padding: 0 10px;
        height: 50px;
        display: flex;
        align-items: center;
        justify-content:space-between;
        .title{
            font-size: 16px;
            font-weight: bold;
        }
    }
    .height-12{
        flex-shrink: 0;
        height: 12px!important;
        background: #f4f5f9
    }

    .edit-content{
        flex: 1;
        overflow: auto;
    }
</style>
